package com.wyix.service.impl;

import com.wyix.model.User;
import com.wyix.service.UserService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author wangdx
 * @since 2025/8/8
 */
@Service
public class UserServiceImpl implements UserService {

    // 模拟数据库
    private static final Map<Integer, User> users = new HashMap<>();
    private static final AtomicInteger counter = new AtomicInteger(3);

    // 初始化一些测试数据
    static {
        users.put(1, new User(1, "张三", "zhangsan@example.com"));
        users.put(2, new User(2, "李四", "lisi@example.com"));
        users.put(3, new User(3, "王五", "wangwu@example.com"));
    }

    @Override
    public List<User> getAllUsers() {
        return new ArrayList<>(users.values());
    }

    @Override
    public User getUserById(Integer id) {
        return users.get(id);
    }

    @Override
    public User createUser(User user) {
        int newId = counter.incrementAndGet();
        user.setId(newId);
        users.put(newId, user);
        return user;
    }

    @Override
    public User updateUser(User user) {
        if (users.containsKey(user.getId())) {
            users.put(user.getId(), user);
            return user;
        }
        return null;
    }

    @Override
    public void deleteUser(Integer id) {
        users.remove(id);
    }
}

